package com.seng.deptservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seng.deptservice.entity.TbDepartment;
import com.seng.deptservice.mapper.TbDepartmentMapper;
import com.seng.deptservice.service.TbDepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author seng
 * @since 2020-07-26
 */
@Service
public class TbDepartmentServiceImpl extends ServiceImpl<TbDepartmentMapper, TbDepartment> implements TbDepartmentService {

    @Override
    public List<TbDepartment> findDeptAndCount() {
        return this.baseMapper.findDeptAndCount();
    }

    /**
     * 部门管理查询所有
     * @param page
     * @param wrapper
     * @return
     */
    @Override
    public IPage<TbDepartment> findStudentPage(Page<TbDepartment> page, QueryWrapper<TbDepartment> wrapper) {
        return this.baseMapper.findStudentPage(page,wrapper);
    }

    /**
     * 查询部门下拉框的值
     * @return
     */
    @Override
    public List<TbDepartment> findUsername() {
        return this.baseMapper.findUsername();
    }

    /**
     * 导出学生信息excel
     *
     * @param response http响应
     */
    @Override
    public void exportStudentInfoExcel(HttpServletResponse response) throws IOException {
        // ----- 获取导出数据 -----
        List<TbDepartment> studentExcelInfo = this.baseMapper.selectList(null);

        //----- 写入excel文件
        String exportFileName = "TbDept.xlsx";
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.ms-excel");
        response.addHeader("Content-Disposition", "attachment;filename=" + exportFileName);
        EasyExcel.write(response.getOutputStream(), TbDepartment.class)
                .sheet("部门信息")
                //自动列宽
                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                .doWrite(studentExcelInfo);
    }
}
